package com.proj.android.canyondefense.core.impl;

import java.io.IOException;
import java.io.InputStream;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory.Options;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;

import com.proj.android.canyondefense.core.base.IGraphics;
import com.proj.android.canyondefense.core.base.IPixmap;

public class AndroidGraphics implements IGraphics {
	private AssetManager _assetMgr;
	private Bitmap _frameBuffer;
	private Canvas _canvas;
	private Paint _paint;
	private Rect _srcRect;
	private Rect _destRect;
	
	public AndroidGraphics(AssetManager assets, Bitmap frameBuffer) {
		this._assetMgr = assets;
		this._frameBuffer = frameBuffer;
		this._canvas = new Canvas(_frameBuffer);
		this._paint = new Paint();
	}

	public IPixmap newPixmap(String fileName, PixmapFormat format) {
		Config config = null;
		switch (format) {
			case RGB565:
				config = Config.RGB_565;
				break;
			case ARGB4444:
				config = Config.ARGB_4444;
				break;
			case ARGB8888:
				config = Config.ARGB_8888;
				break;
		}
		Options option = new Options();
		option.inPreferredConfig = config;
		
		InputStream in = null;
		Bitmap bitmap = null;
		try {
			in = _assetMgr.open(fileName);
			bitmap = BitmapFactory.decodeStream(in);
			if (bitmap == null) {
				throw new RuntimeException("[AndroidGraphics] :: newPixmap - Couldn't load bitmap from asset '" + fileName + "'");
			}
		} catch (IOException e) {
			throw new RuntimeException("[AndroidGraphics] :: newPixmap - Couldn't load bitmap from asset '" + fileName + "'");
		} finally {
			if (in != null) {
				try {
                    in.close();
                } catch (IOException e) {
                	
                }
			}
		}
		
		if (bitmap.getConfig() == Config.RGB_565) {
			format = PixmapFormat.RGB565; 
		} else if (bitmap.getConfig() == Config.ARGB_4444) {
			format = PixmapFormat.ARGB4444;
		} else {
			format = PixmapFormat.ARGB8888;
		}

	    return new AndroidPixmap(bitmap, format);
	}

	public void clear(int color) {
		_canvas.drawRGB((color & 0xFF0000) >> 16, (color & 0xFF00) >> 8, (color & 0xFF));
	} 

	public void drawPixel(int x, int y, int color) {
		_paint.setColor(color);
		_canvas.drawPoint(x, y, _paint);
		
	}

	public void drawLine(int startX, int startY, int stopX, int stopY, int color) {
		_paint.setColor(color);
		_canvas.drawLine(startX, startY, stopX, stopY, _paint);
	}

	public void drawRect(int x, int y, int width, int height, int color) {
		_paint.setColor(color);
		_paint.setStyle(Style.FILL);
		_canvas.drawRect(x, y, x + width - 1, y + height - 1, _paint);
	}

	public void drawPixmap(IPixmap pixmap, int x, int y, int srcX, int srcY, int srcWidth, int srcHeight) {
		_srcRect.left = srcX;
		_srcRect.top = srcY;
		_srcRect.right = srcX + srcWidth - 1;
		_srcRect.bottom = srcY + srcHeight - 1;

		_destRect.left = x;
		_destRect.top = y;
		_destRect.right = x + srcWidth - 1;
		_destRect.bottom = y + srcHeight - 1;
		
		_canvas.drawBitmap(((AndroidPixmap) pixmap).getBitmap(), _srcRect, _destRect, null);
	}

	public void drawPixmap(IPixmap pixmap, int x, int y) {
		_canvas.drawBitmap(((AndroidPixmap)pixmap).getBitmap(), x, y, null);
	}

	public int getWidth() {
		return _frameBuffer.getWidth();
	}

	public int getHeight() {
		return _frameBuffer.getHeight();
	}

}
